﻿using Xejen.Communication.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xejen.Communication
{
    /// <summary>
    /// 针对<see cref="IHeartbeat{THeartbeatConfig}"/>进行扩展
    /// </summary>
    /// <creator>marc</creator>
    public static class HeartbeatExtensions
    {
        /// <summary>
        /// 注册心跳检测器
        /// </summary>
        /// <typeparam name="THeartbeatConfig"><inheritdoc cref="HeartbeatConfig" path="/summary"/></typeparam>
        /// <param name="heartbeat"><inheritdoc cref="IHeartbeat{THeartbeatConfig}" path="/summary"/></param>
        /// <param name="heartbeatChecker"><inheritdoc cref="IHeartbeatChecker" path="/summary"/></param>
        /// <returns></returns>
        public static IHeartbeat<THeartbeatConfig> RegisterHeartbeatChecker<THeartbeatConfig>(this IHeartbeat<THeartbeatConfig> heartbeat, IHeartbeatChecker heartbeatChecker)
            where THeartbeatConfig : HeartbeatConfig, new()
        {
            Check.NotNull(heartbeatChecker, nameof(heartbeatChecker));

            CommunicationHeartbeat<THeartbeatConfig> communicationHeartbeat = heartbeat as CommunicationHeartbeat<THeartbeatConfig>;
            communicationHeartbeat.SetHeartbeatCheckFunction(heartbeatChecker);

            return heartbeat;
        }

        /// <summary>
        /// 注册心跳检测器
        /// </summary>
        /// <typeparam name="THeartbeatConfig"><inheritdoc cref="HeartbeatConfig" path="/summary"/></typeparam>
        /// <typeparam name="THeartbeatChecker"><inheritdoc cref="IHeartbeatChecker" path="/summary"/></typeparam>
        /// <param name="heartbeat"><inheritdoc cref="IHeartbeat{THeartbeatConfig}" path="/summary"/></param>
        /// <returns></returns>
        public static IHeartbeat<THeartbeatConfig> RegisterHeartbeatChecker<THeartbeatConfig, THeartbeatChecker>(this IHeartbeat<THeartbeatConfig> heartbeat)
            where THeartbeatConfig : HeartbeatConfig, new()
            where THeartbeatChecker: IHeartbeatChecker, new()
        {
            THeartbeatChecker heartbeatChecker = new THeartbeatChecker();

            CommunicationHeartbeat<THeartbeatConfig> communicationHeartbeat = heartbeat as CommunicationHeartbeat<THeartbeatConfig>;
            communicationHeartbeat.SetHeartbeatCheckFunction(heartbeatChecker);

            return heartbeat;
        }
    }
}
